Itest = zeros(9);
Itest(1:3, 6:9) = 1;
Itest(4:6, 4:7) = 1;
Itest(7:9, 1:4) = 1;


my1 = [ 0 1 0; 1 1 0; 0 0 0];
my2 = [ 0 0 0; 0 1 1; 0 1 0];
my3 = [ 1 1 0; 1 1 0; 0 0 0];
figure('Name', 'Three asymmetric structuring elements');
subplot(1, 3, 1), imshow(my1, 'InitialMagnification','fit'), title('Element 1');
subplot(1, 3, 2), imshow(my2, 'InitialMagnification','fit'), title('Element 2');
subplot(1, 3, 3), imshow(my3, 'InitialMagnification','fit'), title('Element 3');

myst1 = strel(my1);
myst2 = strel(my2);
myst3 = strel(my3);
Itesterode1 = imerode(Itest,myst1);
Itesterode2 = imerode(Itest,myst2);
Itesterode3 = imerode(Itest,myst3);
figure('Name', 'Erosion using default (same) boundary handling');
subplot(2, 2, 1), imshow(Itest, 'InitialMagnification','fit'), title('Original');
subplot(2, 2, 2), imshow(Itesterode1, 'InitialMagnification','fit'), title('Element 1');
subplot(2, 2, 3), imshow(Itesterode2, 'InitialMagnification','fit'); title('Element 2');
subplot(2, 2, 4), imshow(Itesterode3, 'InitialMagnification','fit'); title('Element 3');

Itesterode1f = imerode(Itest,myst1, 'full');
Itesterode2f = imerode(Itest,myst2, 'full');
Itesterode3f = imerode(Itest,myst3, 'full');
figure('Name', 'Erosion using full boundary handling');
subplot(2, 2, 1), imshow(Itest, 'InitialMagnification','fit'), title('Original');
subplot(2, 2, 2), imshow(Itesterode1f, 'InitialMagnification','fit'), title('Element 1');
subplot(2, 2, 3), imshow(Itesterode2f, 'InitialMagnification','fit'); title('Element 2');
subplot(2, 2, 4), imshow(Itesterode3f, 'InitialMagnification','fit'); title('Element 3');

%Padding to get the traditional erosion
Itestpad1 = padarray(Itest, [3, 3], 0, 'both');
Itestpad2 = padarray(uint8(255*double(Itest)), [3, 3], 128, 'both');
Itestpad1erode = imerode(Itestpad1, myst1);
Itestpad2erode = imerode(Itestpad2, myst1);

figure('Name', 'Erosion with padding');
subplot(2,2,1), imshow(Itestpad1), title('Original binary with 0 pad');
subplot(2,2,2), imshow(Itestpad2), title('Original uint8 with 128 pad');
subplot(2,2,3), imshow(Itestpad1erode), title('Eroded with 0 pad');
subplot(2,2,4), imshow(Itestpad2erode), title('Eroded with 128 pad');
% 
% 
% Itestpad1eu = Itestpad1erode(4:12, 4:12);
% 
% figure('Name', 'Erosion - comparison of boundary');
% subplot(2,2,1), imshow(Itest), title('Original ');
% subplot(2,2,2), imshow(Itestpad1eu), title('Eroded with 0 pad');
% subplot(2,2,3), imshow(Itesterode1f), title('Eroded using full option');
% subplot(2,2,4), imshow(Itesterode1), title('Eroded using same (default) option');


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Itestdilate1 = imdilate(Itest,myst1);
Itestdilate2 = imdilate(Itest,myst2);
Itestdilate3 = imdilate(Itest,myst3);
figure('Name', 'Dilation using default (same) boundary handling');
subplot(2, 2, 1), imshow(Itest, 'notruesize'), title('Original');
subplot(2, 2, 2), imshow(Itestdilate1, 'notruesize'), title('Element 1');
subplot(2, 2, 3), imshow(Itestdilate2, 'notruesize'); title('Element 2');
subplot(2, 2, 4), imshow(Itestdilate3, 'notruesize'); title('Element 3');

Itestdilate1f = imdilate(Itest,myst1, 'full');
Itestdilate2f = imdilate(Itest,myst2, 'full');
Itestdilate3f = imdilate(Itest,myst3, 'full');
figure('Name', 'Dilation using full boundary handling');
subplot(2, 2, 1), imshow(Itest, 'notruesize'), title('Original');
subplot(2, 2, 2), imshow(Itestdilate1f, 'notruesize'), title('Element 1');
subplot(2, 2, 3), imshow(Itestdilate2f, 'notruesize'); title('Element 2');
subplot(2, 2, 4), imshow(Itestdilate3f, 'notruesize'); title('Element 3');

%Padding to get the traditional erosion
Itestpad1 = padarray(Itest, [3, 3], 0, 'both');
Itestpad2 = padarray(uint8(255*double(Itest)), [3, 3], 128, 'both');
Itestpad1dilate = imdilate(Itestpad1, myst1);
Itestpad2dilate = imdilate(Itestpad2, myst1);

figure('Name', 'Dilation with padding');
subplot(2,2,1), imshow(Itestpad1), title('Original binary with 0 pad');
subplot(2,2,2), imshow(Itestpad2), title('Original uint8 with 128 pad');
subplot(2,2,3), imshow(Itestpad1dilate), title('Dilated with 0 pad');
subplot(2,2,4), imshow(Itestpad2dilate), title('Dilated with 128 pad');

% 
% Itestpad1du = Itestpad1dilate(4:12, 4:12);
% 
% figure('Name', 'Dilation - comparison of boundary');
% subplot(2,2,1), imshow(Itest), title('Original ');
% subplot(2,2,2), imshow(Itestpad1du), title('Dilated with 0 pad');
% subplot(2,2,3), imshow(Itestdilate1f), title('Dilated using full option');
% subplot(2,2,4), imshow(Itestdilate1), title('Dilated using same (default) option');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Itestopen1 = imopen(Itest,myst1);
Itestopen2 = imopen(Itest,myst2);
Itestopen3 = imopen(Itest,myst3);
figure('Name', 'Open using default  boundary handling');
subplot(2, 2, 1), imshow(Itest, 'notruesize'), title('Original');
subplot(2, 2, 2), imshow(Itestopen1, 'notruesize'), title('Element 1');
subplot(2, 2, 3), imshow(Itestopen2, 'notruesize'); title('Element 2');
subplot(2, 2, 4), imshow(Itestopen3, 'notruesize'); title('Element 3');


%Padding to get the traditional open
Itestpad1 = padarray(Itest, [3, 3], 0, 'both');
Itestpad2 = padarray(uint8(255*double(Itest)), [3, 3], 128, 'both');
Itestpad1open = imopen(Itestpad1, myst1);
Itestpad2open = imopen(Itestpad2, myst1);

figure('Name', 'Open with padding');
subplot(2,2,1), imshow(Itestpad1), title('Original binary with 0 pad');
subplot(2,2,2), imshow(Itestpad2), title('Original uint8 with 128 pad');
subplot(2,2,3), imshow(Itestpad1open), title('Opened with 0 pad');
subplot(2,2,4), imshow(Itestpad2open), title('Opened with 128 pad');


% Itestpad1ou = Itestpad1open(4:12, 4:12);
% Itestpad1erode = imerode(Itestpad1,myst1);
% Itestpad1eu = Itestpad1erode(4:12, 4:12);
% 
% 
% figure('Name', 'Open - comparison of boundary');
% subplot(2,2,1), imshow(Itest), title('Original ');
% subplot(2,2,2), imshow(Itestpad1ou), title('Opened with 0 pad');
% subplot(2,2,3), imshow(Itestpad1eu), title('Erode with 0 pad');
% subplot(2,2,4), imshow(Itestopen1), title('Opened using same (default) option');

my4 = ones(3);
myst4 = strel(my4);
Itestpad4open = imopen(Itestpad1, myst4);
Itestpad4ou = Itestpad4open(4:12, 4:12);


my5 = ones(5);
myst5 = strel(my5);
Itestpad5open = imopen(Itestpad1, myst5);
Itestpad5ou = Itestpad5open(4:12, 4:12);

% figure('Name', 'Open comparison of different structuring elements (0 pad)');
% subplot(2,2,1), imshow(Itest), title('Original ');
% subplot(2,2,2), imshow(Itestpad1ou), title('Opened with 0 pad');
% subplot(2,2,3), imshow(Itestpad4ou), title('Opened with 3x3');
% subplot(2,2,4), imshow(Itestpad5ou), title('Opened using 5x5');

enam = imread('macchie.tif');
enamcomp = imcomplement(enam);
enamcpad = padarray(enamcomp, [3, 3], 0, 'both');
enamcomp4open = imopen(enamcpad, myst4);
enamcomp5open = imopen(enamcpad, myst5);
enamcomp4ou = enamcomp4open(4:(end-3), 4:(end-3));
enamcomp5ou = enamcomp5open(4:(end-3), 4:(end-3));
my6 = ones(7);
myst6 = strel(my6);
enamcomp6open = imopen(enamcpad, myst6);
enamcomp6ou = enamcomp6open(6:(end-5), 6:(end-5));

figure('Name', 'macchie');
subplot(2, 2, 1), imshow(enam), title('Original');
subplot(2, 2, 2), imshow(enamcomp), title('Complemented');

figure('Name', 'macchie - Opened');
subplot(2, 2, 1), imshow(enamcomp), title('Complemented');
subplot(2, 2, 2), imshow(enamcomp4ou), title('Opened with 3x3');
subplot(2, 2, 3), imshow(enamcomp5ou), title('Opened with 5x5');
subplot(2, 2, 4), imshow(enamcomp6ou), title('Opened with 7x7');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Itestclose1 = imclose(Itest,myst1);
Itestclose2 = imclose(Itest,myst2);
Itestclose3 = imclose(Itest,myst3);
figure('Name', 'Close using default  boundary handling');
subplot(2, 2, 1), imshow(Itest, 'notruesize'), title('Original');
subplot(2, 2, 2), imshow(Itestclose1, 'notruesize'), title('Element 1');
subplot(2, 2, 3), imshow(Itestclose2, 'notruesize'); title('Element 2');
subplot(2, 2, 4), imshow(Itestclose3, 'notruesize'); title('Element 3');


%Padding to get the traditional open
Itestpad1 = padarray(Itest, [3, 3], 0, 'both');
Itestpad2 = padarray(uint8(255*double(Itest)), [3, 3], 128, 'both');
Itestpad1close = imclose(Itestpad1, myst1);
Itestpad2close = imclose(Itestpad2, myst1);

figure('Name', 'Close with padding');
subplot(2,2,1), imshow(Itestpad1), title('Original binary with 0 pad');
subplot(2,2,2), imshow(Itestpad2), title('Original uint8 with 128 pad');
subplot(2,2,3), imshow(Itestpad1close), title('Closed with 0 pad');
subplot(2,2,4), imshow(Itestpad2close), title('Closed with 128 pad');


% Itestpad1cu = Itestpad1close(4:12, 4:12);
% Itestpad1dilate = imdilate(Itestpad1,myst1);
% Itestpad1du = Itestpad1dilate(4:12, 4:12);
% 
% 
% figure('Name', 'Close - comparison of boundary');
% subplot(2,2,1), imshow(Itest), title('Original ');
% subplot(2,2,2), imshow(Itestpad1cu), title('Closed with 0 pad');
% subplot(2,2,3), imshow(Itestpad1du), title('Dilated with 0 pad');
% subplot(2,2,4), imshow(Itestclose1), title('Closed using same (default) option');

my4 = ones(3);
myst4 = strel(my4);
Itestpad4close = imclose(Itestpad1, myst4);
Itestpad4cu = Itestpad4close(4:12, 4:12);


my5 = ones(5);
myst5 = strel(my5);
Itestpad5close = imclose(Itestpad1, myst5);
Itestpad5cu = Itestpad5close(4:12, 4:12);

% figure('Name', 'Close comparison of different structuring elements (0 pad)');
% subplot(2,2,1), imshow(Itest), title('Original ');
% subplot(2,2,2), imshow(Itestpad1cu), title('Closed with 0 pad');
% subplot(2,2,3), imshow(Itestpad4cu), title('Closed with 3x3');
% subplot(2,2,4), imshow(Itestpad5cu), title('Closed using 5x5');




enam = imread('macchie.tif');
enamcomp = imcomplement(enam);
enamcpad = padarray(enamcomp, [5, 5], 0, 'both'); %padded bigger
enamcomp4close = imclose(enamcpad, myst4);
enamcomp5close = imclose(enamcpad, myst5);
enamcomp4cu = enamcomp4close(6:(end-5), 6:(end-5));
enamcomp5cu = enamcomp5close(6:(end-5), 6:(end-5));
my6 = ones(7);
myst6 = strel(my6);
enamcomp6close = imclose(enamcpad, myst6);
enamcomp6cu = enamcomp6close(6:(end-5), 6:(end-5));

figure('Name', 'macchie');
subplot(2, 1, 1), imshow(enam), title('Original');
subplot(2, 1, 2), imshow(enamcomp), title('Complemented');


figure('Name', 'macchie - Closed');
subplot(2, 2, 1), imshow(enamcomp), title('Complemented');
subplot(2, 2, 2), imshow(enamcomp4cu), title('Closed with 3x3');
subplot(2, 2, 3), imshow(enamcomp5cu), title('Closed with 5x5');
subplot(2, 2, 4), imshow(enamcomp6cu), title('Closed with 7x7');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Read cameraman
I = imread('cameraman.tif');
se = strel('ball',5,5);

figure('Name', 'original');
imshow(I);

figure('Name', 'Basic morphological operations 5x5 ball');
Ierode = imerode(I,se);
Idilate = imdilate(I,se);
Iclose = imclose(I,se);
Iopen = imclose(I, se);
subplot(2,2,1), imshow(Ierode), title('Erode');
subplot(2,2,2), imshow(Idilate), title('Dilate');
subplot(2,2,3), imshow(Iopen), title('Open');
subplot(2,2,4), imshow(Iclose), title('Close');

%Look at different structuring elements
se1 = strel('square',11);      % 11-by-11 square
se2 = strel('line',10,45);     % line, length 10, angle 45 degrees
se3 = strel('disk',3);         % disk, radius 3
se4 = strel('ball',15,5);      % ball, radius 15, height 5

figure('Name', 'Erode');
Ierode1 = imerode(I,se1);
Ierode2 = imerode(I,se2);
Ierode3 = imerode(I,se3);
Ierode4 = imerode(I,se4);
subplot(2,2,1), imshow(Ierode1), title('11x11 square');
subplot(2,2,2), imshow(Ierode2), title('45 deg line len 10');
subplot(2,2,3), imshow(Ierode3), title('Disk, radius 3');
subplot(2,2,4), imshow(Ierode4), title('Ball, radius 15, height 5');


figure('Name', 'Dilate');
Idilate1 = imdilate(I,se1);
Idilate2 = imdilate(I,se2);
Idilate3 = imdilate(I,se3);
Idilate4 = imdilate(I,se4);
subplot(2,2,1), imshow(Idilate1), title('11x11 square');
subplot(2,2,2), imshow(Idilate2), title('45 deg line len 10');
subplot(2,2,3), imshow(Idilate3), title('Disk, radius 3');
subplot(2,2,4), imshow(Idilate4), title('Ball, radius 15, height 5');

figure('Name', 'Open');
Iopen1 = imopen(I,se1);
Iopen2 = imopen(I,se2);
Iopen3 = imopen(I,se3);
Iopen4 = imopen(I,se4);
subplot(2,2,1), imshow(Iopen1), title('11x11 square');
subplot(2,2,2), imshow(Iopen2), title('45 deg line len 10');
subplot(2,2,3), imshow(Iopen3), title('Disk, radius 3');
subplot(2,2,4), imshow(Iopen4), title('Ball, radius 15, height 5');


figure('Name', 'Close');
Iclose1 = imclose(I,se1);
Iclose2 = imclose(I,se2);
Iclose3 = imclose(I,se3);
Iclose4 = imclose(I,se4);
subplot(2,2,1), imshow(Iclose1), title('11x11 square');
subplot(2,2,2), imshow(Iclose2), title('45 deg line len 10');
subplot(2,2,3), imshow(Iclose3), title('Disk, radius 3');
subplot(2,2,4), imshow(Iclose4), title('Ball, radius 15, height 5');